package club.lovety.service.user.portal.controller;

import club.lovety.service.user.portal.integration.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/v1")
public class UserPortalController {

    private static final Logger log = LoggerFactory.getLogger(UserPortalController.class);

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private UserService userService;

    @RequestMapping("getuser")
    public Object getUser() {
//        ServiceInstance serviceInstance = loadBalancerClient.choose("user-client");
//        log.info("uri: {}",serviceInstance.getUri());
//        log.info("host: {}",serviceInstance.getHost());
//        log.info("isc: {}",serviceInstance.isSecure());
//        Map<String, String> metadata =  serviceInstance.getMetadata();
//        metadata.entrySet().forEach(e->{
//            log.info("key: {}, value: {}",e.getKey(),e.getValue());
//        });
//        Object obj = restTemplate.getForEntity(serviceInstance.getUri()+"/service/user/loaduser",Object.class);
        Object obj = userService.loadUser();
        return obj;
    }


}
